package com.zwk.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.zwk.model.WKStdLoginOperator;
import com.zwk.model.WKTchOperator;
import com.zwk.views.*;

public class WKResetStdPwdView extends JFrame{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH = 350;
	private static final int DEFAULT_HEIGHT = 200;
	public static JFrame frame;
	JFrame parentFrame;
	public WKResetStdPwdView(String id) {
		frame = new JFrame();
		frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		frame.setTitle("修改个人密码");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidthpx = screenSize.width;
		int screenHeightpx = screenSize.height;
		frame.setLocation(screenWidthpx / 3, screenHeightpx / 3);
		frame.setLocationByPlatform(false);
		
		Image image = new ImageIcon(this.getClass().getResource("/WKImage/top1.jpg")).getImage();
		frame.setIconImage(image);
		
		WKResetStdPwdPanel loginPanel = new WKResetStdPwdPanel(id);
		frame.setContentPane(loginPanel);
		frame.setLayout(new BorderLayout());
		frame.setVisible(true);
		
	}
	private class WKResetStdPwdPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel currentLabel;
	    JPasswordField currentField;
	    JLabel changeLabel;
	    JPasswordField changeField;
	    JLabel sureLabel;
	    JPasswordField sureField;
	    JButton okButton, backButton;
	    JPanel buttonPanel, resetPanel;
	    
	    public WKResetStdPwdPanel(final String id) {
	    	resetPanel = new JPanel();
	    	resetPanel.setBounds(0, 0, 350, 140);
	    	resetPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
	    	resetPanel.setBorder(BorderFactory.createTitledBorder("修改密码"));
	    	currentLabel = new JLabel("当前密码");
	    	currentField = new JPasswordField(20);
	    	changeLabel = new JLabel("   新密码");
	    	changeField = new JPasswordField(20);
	    	sureLabel = new JLabel("确认密码");
	    	sureField = new JPasswordField(20);
	    	resetPanel.add(currentLabel);
	    	resetPanel.add(currentField);
	    	resetPanel.add(changeLabel);
	    	resetPanel.add(changeField);
	    	resetPanel.add(sureLabel);
	    	resetPanel.add(sureField);
	    	add(resetPanel, BorderLayout.CENTER);
	    	
	    	buttonPanel = new JPanel();
	    	buttonPanel.setBounds(0, 140, 350, 50);
	    	okButton = new JButton("确认修改");
	    	okButton.addActionListener(new ActionListener() {
	    		@SuppressWarnings("deprecation")
				@Override
	    		public void actionPerformed(ActionEvent e) {
	    			if (!changeField.getText().equals(sureField.getText()))
	    			{
	    				JOptionPane.showMessageDialog(null, "两次密码不一致", "修改失败", JOptionPane.ERROR_MESSAGE);
	    			} else {
	    				if (id.equals("2014213913"))
		    			{
	    					WKStdLoginOperator OP = new WKStdLoginOperator();
		    			    OP.resetStdPwd(id, changeField.getText());
		    			    JOptionPane.showMessageDialog(null, "修改信息成功", "修改成功", JOptionPane.OK_OPTION);
		    			} else {
		    				WKTchOperator OP = new WKTchOperator();
		    			    OP.resetTchPwd(id, changeField.getText());
		    			    JOptionPane.showMessageDialog(null, "修改信息成功", "修改成功", JOptionPane.OK_OPTION);
		    			
		    			}
	    			}
	    		}
	    	});
	    	backButton = new JButton("返回上级菜单");
	    	backButton.addActionListener(new ActionListener() {
	    		@Override
	    		public void actionPerformed(ActionEvent e) {
	    			if (id.equals("2014213913"))
	    			{
	    			    WKStdFuncPortalFrame stdView = new WKStdFuncPortalFrame(id);
	    			    frame.dispose();
	    			} else {
	    				WKTchFuncPortalFrame tchView = new WKTchFuncPortalFrame(id);
	    				frame.dispose();
	    			}
	    		}
	    	});
	    	buttonPanel.add(okButton);
	    	buttonPanel.add(backButton);
	    	add(buttonPanel, BorderLayout.SOUTH);
	    	
	    	    	
	    }
	
	}
}